package com.service.pi;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;
import com.dao.pi.WwytDao;
import com.model.pi.WwytModel;
import com.model.sys.SysUser;

@Service("piWwytService")
public class WwytService {
	static final Logger logger = LoggerFactory.getLogger(WwytService.class);

	@Autowired
	WwytDao wwytDao;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<WwytModel> list = wwytDao.selectList(queryMap);
		int count = wwytDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String indexCode,String createTime) {
		HttpResult httpResult = new HttpResult();
		WwytModel wwytModel = wwytDao.load(indexCode,createTime);
		httpResult.setData(wwytModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult save(WwytModel wwytModel) {
		if (wwytModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(wwytModel.getIndexCode())) {
			wwytModel.setIndexCode(UUID.randomUUID().toString());
			result = wwytDao.insert(wwytModel);
		} else {
			result = wwytDao.update(wwytModel);
		}
		if (!StringUtils.isNotNull(wwytModel.getCreateTime())) {
			wwytModel.setCreateTime(UUID.randomUUID().toString());
			result = wwytDao.insert(wwytModel);
		} else {
			result = wwytDao.update(wwytModel);
		}
		if (result > 0) {
//			httpResult.setMsg(wwytModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(wwytModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
//			httpResult.setMsg(wwytModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 提交
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult commit(WwytModel wwytModel) {
		if (wwytModel == null) {
			return HttpResult.error("参数不能为空");
		}
//		wwytModel.setStatus(Global.YES+"");
		HttpResult httpResult = new HttpResult();
		int result = 0;
			result = wwytDao.update(wwytModel);
			result = wwytDao.update(wwytModel);
		if (result > 0) {
//			httpResult.setMsg(wwytModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(wwytModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
//			httpResult.setMsg(wwytModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult delete(WwytModel wwytModel) {
		if (!StringUtils.isNotNull(wwytModel.getIndexCode())||!StringUtils.isNotNull(wwytModel.getCreateTime())) {
			return HttpResult.error("参数不能为空");
		}
		WwytModel model = wwytDao.load(wwytModel.getIndexCode(),wwytModel.getCreateTime());
		if(model==null){
			return HttpResult.error("无数据！");
		}
//		if (!model.getStatus().equals(Global.NO + "")) {
//			return HttpResult.error("非待提交状态，不可删除！");
//		}		
		HttpResult httpResult = new HttpResult();
		int result = wwytDao.delete(wwytModel.getIndexCode(),wwytModel.getCreateTime());
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 撤回
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult back(String indexCode,String createTime) {
		if (!StringUtils.isNotNull(indexCode)||!StringUtils.isNotNull(createTime)) {
			return HttpResult.error("参数不能为空");
		}
		WwytModel model = wwytDao.load(indexCode,createTime);
		
		//todo	
		
		HttpResult httpResult = new HttpResult();
//		model.setStatus(Global.NO + "");
		int result = wwytDao.update(model);
		if (result > 0) {
			httpResult.setMsg("撤回成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<WwytModel> list = wwytDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), WwytModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
